<?php 
namespace app\index\controller;
use app\index\controller\Common;
use think\Request;
use think\Db;

class Team extends Common{
    public function index(){
        $ind_sub = Db::name('nav')->where('sid', 5)->select();
        $banner = Db::name('banner')->where('is_show', 1)->field('img_url,link_url')->limit(1)->find();
        $banner = empty($banner) ? null : $banner;

        $team_type = Request::instance()->param('teamType');
        if(empty($team_type)){
            $teams = Db::name('team')->paginate(6);
        }else{
            $teams = Db::name('team')->where('nid', $team_type)->paginate(6);
        }

        $team_arr = [];
        foreach($teams as $key => $per){
            $team_arr[$key]['tid'] = $per['tid'];
            $team_arr[$key]['title'] = mb_substr($per['title'], 0, 60);
            $team_arr[$key]['name'] = $per['name'];
            $team_arr[$key]['intro'] = mb_substr($per['intro'], 0, 150);
            $team_arr[$key]['img_url'] = $per['img_url'];
        }

        $page = $teams->render();

        $this->assign('page',$page);
        $company = [
            'sub' => $ind_sub,
            'banner' => $banner,
            'teams' => $team_arr,
        ];
        $this->assign('team', $company);
        return $this->fetch('team');
    }

    public function teamDetail($tid){
        $banner = Db::name('banner')->where('is_show', 1)->field('img_url,link_url')->limit(1)->find();
        $banner = empty($banner) ? null : $banner;
        $ind_sub = Db::name('nav')->where('sid', 3)->select();

        $person = Db::name('team')->field('img_url,name,intro,title')->where('tid',$tid)->find();
        $team = Db::name('team')->alias('t')->join('nav n', 't.nid = n.nid')->join('case c', 't.tid = c.tid')->field('n.nname,c.img_url')->where('t.tid', $tid)->select();
        $prev = Db::name('team')->where('tid', '<', $tid)->field('tid,name')->limit(1)->find();
        $next = Db::name('team')->where('tid', '>', $tid)->field('tid,name')->limit(1)->find();

        $company = [
            'sub' => $ind_sub,
            'banner' => $banner,
            'team' => $team,
            'person' => $person,
            'prev' => $prev,
            'next' => $next,
        ];
        $this->assign('detail', $company);
        return $this->fetch('team_detail');
    }
}